class MigrateEditorGroups < ActiveRecord::Migration
  
  class << self
   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   # Data insert/delete methods
   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def up
      drop_lookup_table(:documents,:user_groups)

      init_roles
      add_acl_data
    end
    
    def down
      init_roles
      delete_acl_data
    end

    private
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # Private data insert/delete methods
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      def init_roles
        @@admin = Role.find(1)
      end

      def add_acl_data
        add_editor_group_management_data
        create_editor_groups_table
      end

      def create_editor_groups_table
        create_table_with_privies(:editor_groups, :force => true) do |t|
          t.column("name", :text)
        end

        create_lookup_table(:editor_groups,:users)
        create_lookup_table(:document_categories,:editor_groups)
        create_lookup_table(:editor_groups,:pages)
      end


      def add_editor_group_management_data
        methods = ["manage/permissions/list_editor_groups",
                    "manage/permissions/create_editor_group",
                    "manage/permissions/show_editor_group",
                    "manage/permissions/update_editor_group",
                    "manage/permissions/delete_editor_group"]

        initialize_system_object("Editor Group Management",methods, @@admin)
      end

      def delete_acl_data
        delete_editor_group_management_data
        delete_editor_groups_table
      end

      def delete_editor_group_management_data
        remove_system_object("Editor Group Management",@@admin)
      end

      def delete_editor_groups_table
        drop_lookup_table(:editor_groups,:users)
        drop_lookup_table(:document_categories,:editor_groups)
        drop_lookup_table(:editor_groups,:pages)
        drop_table(:editor_groups)
      end

  end #class block
end
